package cn.wolfcode.wolf2world.web.controller;

import cn.wolfcode.wolf2world.domain.Destination;
import cn.wolfcode.wolf2world.domain.Strategy;
import cn.wolfcode.wolf2world.domain.Travel;
import cn.wolfcode.wolf2world.domain.UserInfo;
import cn.wolfcode.wolf2world.elasitcsearch.domain.DestinationEs;
import cn.wolfcode.wolf2world.elasitcsearch.domain.StrategyEs;
import cn.wolfcode.wolf2world.elasitcsearch.domain.TravelEs;
import cn.wolfcode.wolf2world.elasitcsearch.domain.UserInfoEs;
import cn.wolfcode.wolf2world.elasitcsearch.service.DestinationEsService;
import cn.wolfcode.wolf2world.elasitcsearch.service.StrategyEsEsService;
import cn.wolfcode.wolf2world.elasitcsearch.service.TravelEsService;
import cn.wolfcode.wolf2world.elasitcsearch.service.UserInfoEsService;
import cn.wolfcode.wolf2world.service.DestinationService;
import cn.wolfcode.wolf2world.service.StrategyService;
import cn.wolfcode.wolf2world.service.TravelService;
import cn.wolfcode.wolf2world.service.UserInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 杨兴健
 * @Date 2020/8/20 13:20
 */
@RestController
public class DateInitController {
    @Autowired
    private DestinationService destinationService;
    @Autowired
    private StrategyService strategyService;
    @Autowired
    private TravelService travelService;
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private DestinationEsService destinationEsService;
    @Autowired
    private StrategyEsEsService strategyEsEsService;
    @Autowired
    private TravelEsService travelEsService;
    @Autowired
    private UserInfoEsService userInfoEsService;


    @GetMapping("/dateInit")
    public String dateInit() {

        //初始化目的地
        List<Destination> destinations = destinationService.list();
        DestinationEs destinationEs = new DestinationEs();
        destinations.forEach(destination -> {
            BeanUtils.copyProperties(destination,destinationEs);
            destinationEsService.save(destinationEs);
        });

        //初始化攻略
        List<Strategy> strategies = strategyService.list();
        StrategyEs strategyEs = new StrategyEs();
        for (Strategy strategy : strategies) {
            BeanUtils.copyProperties(strategy,strategyEs);
            strategyEsEsService.save(strategyEs);
        }

        //初始化游记
        List<Travel> travels = travelService.list();
        TravelEs travelEs = new TravelEs();
        travels.forEach(travel -> {
            BeanUtils.copyProperties(travel,travelEs);
            travelEsService.save(travelEs);
        });

        //初始化用户
        List<UserInfo> userInfos = userInfoService.list();
        UserInfoEs userInfoEs = new UserInfoEs();
        userInfos.forEach(userInfo -> {
            BeanUtils.copyProperties(userInfo,userInfoEs);
            userInfoEsService.save(userInfoEs);
        });
        return "初始化完毕";
    }
}
